cmake_minimum_required(VERSION 3.20)
project(BoostLearning)

if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
    message(STATUS "Windows Detected!")
    # 如果是 Windows，设置 Boost 根目录
    set(Boost_ROOT "C:/boost")
    message(STATUS "set Boost_ROOT to ${Boost_ROOT}")
endif()

find_package(Boost REQUIRED)
set(Boost_NO_SYSTEM_PATHS ON) # 只搜索上语句设置的搜索路径

if(Boost_FOUND)
    message(STATUS "Boost found!")
    message(STATUS "Boost_LIB_VERSION = ${Boost_LIB_VERSION}.")
endif()

set(Boost_ARCHITECTURE “-x64”)

# 添加一个接口库, 设置c++20标准
add_library(BoostLearning_compile_flags INTERFACE)
target_compile_features(BoostLearning_compile_flags INTERFACE cxx_std_20)

# 设置-Wall标准
add_library(Warning_compile_flags INTERFACE)

# 设置 -Wall 和 -Wextra 以及 MSVC 的 /W4
if (MSVC)
    message(STATUS "add MSVC warning options '/W4'")
    target_compile_options(Warning_compile_flags INTERFACE /W4)
else()
    message(STATUS "add non-MSVC warning options '-Wall -Wextra -Wpedantic'")
    target_compile_options(Warning_compile_flags INTERFACE -Wall -Wextra -Wpedantic)
endif()

# 遍历章节目录
file(GLOB CHAPTER_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/chapter*")

# 遍历每个章节
foreach(CHAPTER_DIR ${CHAPTER_DIRS})
    # 获取文件夹名称
    get_filename_component(CHAPTER_DIR_LAST_WORD ${CHAPTER_DIR} NAME)

    # 获取章节中的所有CPP文件
    file(GLOB CHAPTER_CPP_FILES "${CHAPTER_DIR}/*.cpp")

    # 遍历每个CPP文件
    foreach(CPP_FILE ${CHAPTER_CPP_FILES})
        # 获取CPP文件名，假设文件名的命名规则为"filename.cpp"
        get_filename_component(FILE_NAME ${CPP_FILE} NAME_WE)

        set(EXECUTABLE_NAME "${FILE_NAME}")

        # 添加可执行文件
        add_executable(${EXECUTABLE_NAME} ${CPP_FILE})

        # 链接Boost库和一些接口库
        target_link_libraries(${EXECUTABLE_NAME} PUBLIC
                # Boost
                Boost::boost

                # 接口库 (Interface Libraries)
                BoostLearning_compile_flags
                Warning_compile_flags
        )

        # 设置可执行文件的输出目录为相对路径
        set_target_properties(${EXECUTABLE_NAME} PROPERTIES
                OUTPUT_NAME ${FILE_NAME}
                RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${CHAPTER_DIR_LAST_WORD}
        )
    endforeach()
endforeach()
